/**
 * 
 */
package edu.byu.nlp.pipes;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.logging.Logger;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;

import edu.byu.nlp.io.Files2;
import edu.byu.nlp.io.Paths;

/**
 * @author robbie
 *
 */
public class IndexFileToLabeledFileList implements OneToManyInstanceFunction<String, String, String, String> {

	private static Logger logger = Logger.getLogger(IndexFileToLabeledFileList.class.getName());
	
	private final File basedir;
	private final Charset charset;
	
	public IndexFileToLabeledFileList(File basedir) {
		this(basedir, Charset.defaultCharset());
	}
	
	public IndexFileToLabeledFileList(File basedir, Charset charset) {
		Preconditions.checkNotNull(charset);
		
		this.basedir = basedir;
		this.charset = charset;
	}

	@Override
	public Iterator<Instance<String, String>> apply(final Instance<String, String> indexFilename) {
		try {
			logger.info("Processing " + indexFilename.getData());
			final String label = Paths.splitExtension(indexFilename.getData()).getFirst();
			Iterable<String> lineIterable = Files2.open(new File(basedir, indexFilename.getData()), charset);
			return Iterators.transform(lineIterable.iterator(), new Function<String, Instance<String, String>>() {

				@Override
				public Instance<String, String> apply(final String filedata) {
					return new BasicInstance<String, String>(
							label,
							new File(indexFilename.getSource(), indexFilename.getData()).toString(),
							filedata);
				}
				
			});
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	
}
